package com.wing.tools.screenlive.encoder;

import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.ConfigurationInfo;
import android.opengl.EGL14;
import android.opengl.EGLContext;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.annotation.RequiresApi;
import android.util.Log;

import com.wing.tools.screenlive.encoder.SurfaceManager;
import com.wing.tools.screenlive.encoder.TextureManager;
import com.wing.tools.screenlive.encoder.VideoEncodeWorker;
import com.wing.tools.screenlive.encoder.VideoEncoder;

/**
 * Created by wejust on 2018/1/4.
 */

public class TextureEncoder extends VideoEncoder {
    private final int MESSAGE_CREATE_TEXTURE = 1;
    private final int MESSAGE_CREATE_PROGAME = 3;
    private final int MESSAGE_DRAW_FRAME = 2;

    private static final String TAG = TextureEncoder.class.getSimpleName();
    private EGLContext mSharedContext;
    private int mGLESVersion = 3;

    private SurfaceManager mSurfaceManager;
    private TextureManager mTextureManager;
    private Context mContext;

    private int mCurrentTextureID;
    public TextureEncoder(Context ctx, VideoEncodeWorker.IReceiver receiver) {
        super(receiver);
        this.mContext = ctx;
    }

    public void init(EGLContext sharedContext){
        mSharedContext = sharedContext;
        if(sharedContext == null){
            mSharedContext = EGL14.EGL_NO_CONTEXT;
        }

        ActivityManager am =(ActivityManager) this.mContext.getSystemService(Context.ACTIVITY_SERVICE);
        ConfigurationInfo info = am.getDeviceConfigurationInfo();
//      info.reqGlEsVersion= 0x00010001  //代表opengles 1.1
//      info.reqGlEsVersion=  0x00020000     //代表opengles 2.0
        mGLESVersion = info.reqGlEsVersion >> 0x10;

        mTextureManager = new TextureManager();
        mTextureManager.init();
        mSurfaceManager = new SurfaceManager(mWorker.getSurface(), mSharedContext, mGLESVersion);
    }

    private long computePresentationTimeNsec(int frameIndex) {
        final long ONE_BILLION = 1000000000;
        return frameIndex * ONE_BILLION / mFPS;
    }

    private int _createTexture(){
        return mTextureManager.createTexture();
    }

    public void setTextureID(int id){
        mTextureManager.setTextureId(id);
    }

    public int getTextureID(){
        return mTextureManager.getTextureId();
    }

    private void _drawFrame(int texID){
        mIndex++;

        mSurfaceManager.makeCurrent();
        mTextureManager.drawFrame(texID);
        mSurfaceManager.setPresentationTime(computePresentationTimeNsec(mIndex));
        mSurfaceManager.swapBuffer();

        mCurrentTextureID = -1;
    }

    public void createTexture(){
        mHandler.sendEmptyMessage(MESSAGE_CREATE_TEXTURE);
    }

    public void drawFrame(int texID){
        mCurrentTextureID = texID;
        mHandler.sendEmptyMessage(MESSAGE_DRAW_FRAME);
    }

    @Override
    protected void release(){
        super.release();

        if(mSurfaceManager != null){
            mSurfaceManager.release();
            mSurfaceManager = null;
        }

        if(mTextureManager != null){
            mTextureManager.release();
            mTextureManager = null;
        }
    }

    @Override
    protected void onMessage(Message msg){
        switch (msg.what){
            case MESSAGE_CREATE_TEXTURE:
                _createTexture();
                break;
            case MESSAGE_DRAW_FRAME:
                _drawFrame(mCurrentTextureID);
                break;
        }
    }
}
